package com.house.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.house.common.pojo.EUDataGridResult;
import com.house.pojo.Housefloor;
import com.house.pojo.Housefloor;
import com.house.service.HousefloorService;

@Controller
public class HousefloorController {
	
	@Autowired
	private HousefloorService housefloorService;
	
	@RequestMapping("/floor/list")
	@ResponseBody
	public EUDataGridResult getFloorList(Integer page,Integer rows){
		
		EUDataGridResult result = housefloorService.getFloorList(page, rows);
		
		return result;
	}
	
	@RequestMapping("/floor/delete")
	@ResponseBody
	public String deletefloor(String id){
		String []ids = id.split(",");
		List<Integer> list = new ArrayList<Integer>();
		for(String i:ids){
			list.add(Integer.parseInt(i));
		}
		housefloorService.delete(list);
		return "success";
	}
	@RequestMapping("/floor/save")
	@ResponseBody
	public String addUser(HttpServletRequest request,
			MultipartFile floorPicture) throws Exception {
		Housefloor floor = new Housefloor();
		floor.setFloorKind(request.getParameter("floorKind"));
		floor.setFloorPrice(Integer.parseInt(request.getParameter("floorPrice")));
		floor.setFloorColor(request.getParameter("floorColor"));
		if(floorPicture !=null){
			//存储图片的路径
			String pic_path = "C:\\Users\\emperor\\workspace\\house-manager\\house-manager-web\\src\\main\\webapp\\WEB-INF\\images\\";
			//拿到图片的原始名称
			String originalFilename= floorPicture.getOriginalFilename();
			//图片的新名称
			String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
			File newFile = new java.io.File(pic_path + newFileName);
			//将内存中的数据写入磁盘
			floorPicture.transferTo(newFile);
			floor.setFloorPicture( "images/"+newFileName);	
		}
		housefloorService.add(floor);
		

		return "success";
	}

}
